package com.fly.glide;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.backends.okhttp.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.picasso.Picasso;

public class SingleImgActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 使用OKHttp作为底层网络 默认HttpUrlConnection
        // 使用OKHttp还需要 在 gradle里面配置
//        compile "com.facebook.fresco:fresco:0.6.0"
//        compile 'com.facebook.fresco:imagepipeline-okhttp:0.6.0'
        SimpleProgressiveJpegConfig jpegConfig = new SimpleProgressiveJpegConfig();
        ImagePipelineConfig imagePipelineConfig = OkHttpImagePipelineConfigFactory.
                newBuilder(this, new OkHttpClient()).
                setProgressiveJpegConfig(jpegConfig). // 渐进式JPEG图
                build();
        // 可以用于清空缓冲
        ImagePipeline imagePipeline= Fresco.getImagePipeline();
        // 这句代码必须放在setContentView之前
        Fresco.initialize(this, imagePipelineConfig);
        setContentView(R.layout.activity_single_img);
        final Glide mGlide = Glide.get(getApplicationContext()); // 获取单例对象
        MulBitmapTransformation mulBitmapTransformation =
                new MulBitmapTransformation(this, MulBitmapTransformation.TYPE_NORMAL);
        mulBitmapTransformation.setRadius(50);
        Glide.with(this). // Context Activity FragmentActivity Fragment
                load("http://g.hiphotos.baidu.com/image/h%3D200/sign=efebe5305b6034a836e2bf81fb1249d9/d31b0ef41bd5ad6eab0a08db86cb39dbb6fd3c0b.jpg"). // 加载地址
                placeholder(R.mipmap.ic_launcher). // 占位图
                error(R.mipmap.baidu). // 错误图
//                crossFade(5000). // 控制图片显示的动画 可以指定动画时间 单位毫秒
                diskCacheStrategy(DiskCacheStrategy.NONE).
                skipMemoryCache(false).
                transform(mulBitmapTransformation).
                override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL). // 设置加载的尺寸
                into((ImageView) findViewById(R.id.id_img_glide)); // 显示图片的控件

        Picasso mPicasso = Picasso.with(this);
        mPicasso.setIndicatorsEnabled(true); // 打开图片来源指示
        // 红色-网络 黄色-磁盘 绿色-内存
//        Picasso.with(this). // Context
        mPicasso.load("http://g.hiphotos.baidu.com/image/h%3D200/sign=efebe5305b6034a836e2bf81fb1249d9/d31b0ef41bd5ad6eab0a08db86cb39dbb6fd3c0b.jpg").
                placeholder(R.mipmap.ic_launcher).
                error(R.mipmap.baidu).
                into((ImageView) findViewById(R.id.id_img_picasso));

        findViewById(R.id.id_clear_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mGlide.clearMemory();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mGlide.clearDiskCache();
                    }
                }).start();
            }
        });

        findViewById(R.id.id_clear_single_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Glide.clear(findViewById(R.id.id_img_glide)); // 清除已加载的视图资源，取消有待处理的请求
            }
        });

        Uri uri = Uri.parse("http://www.fresco-cn.org/static/fresco-logo.png");
        PipelineDraweeControllerBuilder controllerBuilder = Fresco.newDraweeControllerBuilder();
        controllerBuilder.setTapToRetryEnabled(true); // 点击重试加载 可以进行4次操作 全部失败显示加载失败图片
        controllerBuilder.setUri(uri);

        DraweeController controller = controllerBuilder.build();
        GenericDraweeHierarchyBuilder builder = GenericDraweeHierarchyBuilder.newInstance(getResources());
        builder.setProgressBarImage(new ProgressBarDrawable()); // 设置加载进度
        builder.setFailureImage(new ColorDrawable(Color.RED)); // 设置失败红色图片
        builder.setPlaceholderImage(new ColorDrawable(Color.GREEN)); // 设置绿色占位图
        builder.setRetryImage(new ColorDrawable(Color.BLUE)); // 加载失败显示蓝色图片
        /**
         * setRetryImage
         * setFailureImage
         * 区别
         * setTapToRetryEnabled 该方法设置为true
         * 在首次加载失败，可以重复进行4次点击重新加载 这个时候显示的是setRetryImage设置的图片
         * 当加载完全失败
         * 1. setTapToRetryEnabled false 加载失败即显示
         * 2. setTapToRetryEnabled true 4次重试加载也失败了即显示
         * */

        GenericDraweeHierarchy hierarchy = builder.build();
        SimpleDraweeView simpleDraweeView = (SimpleDraweeView) findViewById(R.id.id_fresco_sdv);
        // 设置圆角
        /**
         * 注意事项
         * 只有BitmapDrawable ColorDrawable 可以
         * */
        RoundingParams roundingParams = new RoundingParams();
//        roundingParams.setCornersRadius(20); // 圆角 可以分别设置四个圆角
        roundingParams.setRoundAsCircle(true); // 圆圈
        roundingParams.setBorder(Color.GREEN, 2.0f); // 设置边框
        hierarchy.setRoundingParams(roundingParams);


        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri).
                setProgressiveRenderingEnabled(true). // 允许渐进式加载JPEG图片
                build();
        PipelineDraweeController pipelineDraweeController = (PipelineDraweeController)
                Fresco.newDraweeControllerBuilder().
                        setImageRequest(imageRequest).
                        setOldController(controller). // 在指定一个新的controller的时候，使用setOldController，这可节省不必要的内存分
                        setAutoPlayAnimations(true). // 自动动画
                        build();

        // 注意同一个view不要多次调用该方法
        simpleDraweeView.setHierarchy(hierarchy);

        simpleDraweeView.setController(pipelineDraweeController);

//        simpleDraweeView.setImageURI(uri);
    }

}
